#include "xmledit.h" 
#include "tuxdocument.h"
#include "winassert.h"

namespace view
{
	using doc::TuxDocument;
	using win::Window;
	using win::ChildWindow;
	using sys::Rect;
	using win::Window;
	
	LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT msg,
 								  WPARAM wParam, LPARAM lParam)
	{
		static Window w;
		static XmlEdit* view = 0;
		w.Attach(hwnd);
		return DefWindowProc( hwnd,msg,wParam,lParam );
	}
	

	void XmlEdit::Create(const Window &parent,
	                         const int child_index)
	{
	     _child_index = child_index;
	     const HWND parent_handle = parent.GetHandle();
	     const HINSTANCE instance_handle = reinterpret_cast<HINSTANCE>
	         							(GetWindowLong(parent_handle,GWL_HINSTANCE));
		 // Register editor window
		 static const char * EDIT_CLASS = "xml_editor";
	
		 WNDCLASS wc;
		 ZeroMemory(&wc,sizeof(wc));
		 wc.style = CS_HREDRAW|CS_VREDRAW;
		 wc.hInstance = instance_handle;
		 wc.hbrBackground = (HBRUSH) NULL;
//		 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
		 wc.lpfnWndProc = ChildWndProc;
		 wc.lpszClassName = EDIT_CLASS;
		 if (!RegisterClass(&wc))
		 	throw runtime_error("Register class failed");
	
	 	_hwnd = CreateWindowEx( WS_EX_CONTROLPARENT,
                                EDIT_CLASS,NULL,
	  							WS_CHILD | WS_VISIBLE | WS_BORDER ,
	  							0,0,0,0,
	         					parent_handle,
	         					reinterpret_cast<HMENU> (_child_index),
	  							instance_handle,
	  							static_cast<LPVOID> (this));
		if (_hwnd == NULL)
			throw runtime_error("Create window failed");
	  	SetPointer(this);	
		_editbox.Create(*this,1);
		_editbox.SetEnable(false);
		TuxDocument::Instance().Attach(this);
	/*	TEXTMETRIC tm;
		HDC hdc = GetDC(_hwnd);
		GetTextMetrics(hdc,&tm);
		ReleaseDC(_hwnd,hdc);
		_yChar = tm.tmHeight + tm.tmExternalLeading;
		_xChar = tm.tmAveCharWidth; */
	}
	
	void XmlEdit::OnPaint()
	{
	/*   	PAINTSTRUCT  ps;
		HDC hdc = BeginPaint(_hwnd,&ps);
		const char * m = _doc.GetFileName();
		TextOut(hdc,0,0,m,strlen(m));
		EndPaint(_hwnd,&ps); */
	}
	
	// called when the window resizes
	void XmlEdit::OnSize(const int x, const int y)
	{
	/*     // setup vertical scrollbar
	     //cyMax = max( 0, NUM_LINES + 2 - cyWin / cyChr );
	     //cyPos = min( cyPos, cyMax );
	     SetScrollRange( _hwnd, SB_VERT, 0, 10, FALSE );
	     SetScrollPos(   _hwnd, SB_VERT,    1, TRUE );
	     // setup horizontal scrollbar
	     //cxMax = max( 0, NUM_LINES + 60 - cxWin / cxChr );
	     //cxPos = min( cxPos, cxMax );
	     SetScrollRange( _hwnd, SB_HORZ, 0, 10, FALSE );
	     SetScrollPos(   _hwnd, SB_HORZ,    1, TRUE ); */
	}
	
	void XmlEdit::AdjustLayout(int x, int y, int w, int h)
	{
		MoveWH(x,y,w,h);
	    _editbox.MoveWH(0,0,w,h);
	}
	
	void XmlEdit::Update(Observable*,int aspect)
	{
		switch (aspect)
		{
			case TuxDocument::ASPECT_FOCUS_SAVE:
	   			ASSERT(TuxDocument::Instance().IsNodeSelected());
				ASSERT(_editbox.GetText());
				if (_editbox.IsModified())
					TuxDocument::Instance().FocusNode()->SetText(_editbox.GetText());
				break;
			case TuxDocument::ASPECT_FOCUS_OPEN:
	  			_editbox.SetText(TuxDocument::Instance().FocusNode()->Text().c_str());
  				_editbox.SetEnable(TuxDocument::Instance().FocusNode()->NodeStructure().HasText());
				break;
			case TuxDocument::ASPECT_FILE:	
	  			_editbox.SetText("");
  				_editbox.SetEnable(false);
  				break;
		}		
	}

	void XmlEdit::SetBoxFont(HFONT font)
	{
		
//		_editbox.SetEnable(true);
		_editbox.SetFontHandle(font);
//		if (TuxDocument::Instance().IsNodeSelected())
//		   _editbox.SetEnable(TuxDocument::Instance().FocusNode()->NodeStructure().HasText());
//		else   
//	 		_editbox.SetEnable(false); 
	}

}

